<?php

include( "./admin/include/func.php");

if ( ! User::is_logged_in()) header ('Location: index.php');

$db = new db();
if ( ! $db->connect()) exit( mysql_error());

$panels = array ();

$panel = array (
	'caption' => 'Profilom',
	'title'   => 'Profilom',
	'body'    => '',
	'height'  => '330'
);

array_push( $panels, $panel);

if (isset($_POST['modosit'])) {
	
	//debug($_POST);

    $p_bejnev  = trim(strip_tags($_POST['bejnev']));	
    $p_nev     = trim(strip_tags($_POST['nev']));
    $p_telefon = trim(strip_tags($_POST['telefon']));
    $p_varos   = trim(strip_tags($_POST['varos']));
    $p_regio   = (int)$_POST['regio'];
    $p_rovat   = (int)$_POST['rovat'];
    $p_weblap  = trim(strip_tags($_POST['weblap']));

    $success = true;
    $errors = array();

/*    
    if ($p_bejnev == '') {
        $success = false;
        array_push($errors, "A Felhasználó név nincs megadva!");
    } 
   
    if (($p_bejnev) != '' && !preg_match('/^[\s\S]{0,20}$/u', $p_felh_nev)) {
        $success = false;
        array_push($errors, "A Felhasználó név max 20 karakter lehet!");
    }
*/

    if (($p_regio) != '' && !preg_match('/^[0-9]{0,10}$/', $p_regio)) {
        $success = false;
        array_push($errors, "Hibás Régió!");
    }

    if (($p_rovat) != '' && !preg_match('/^[0-9]{0,10}$/', $p_rovat)) {
        $success = false;
        array_push($errors, "Hibás Rovat!");
    }

	if ('' != $p_weblap && 0 !== strpos ($p_weblap, 'http://')) $p_weblap = 'http://' . $p_weblap;
		
    if (($p_weblap) != '' && !preg_match('/^((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}((:[0-9]{1,5})?\/.*)?$/i', $p_weblap)) {
        $success = false;
        array_push($errors, "Hibás Weblap!");
    }
       
    if ( ! $success) {

		$body    = "<ul class='errors'>";
		foreach ($errors as $err) $body .= "<li>$err</li>";
		$body   .= "</ul>";
		
		$error_panel = array (
			'caption' => 'Hiba',
			'title'   => 'Kérjük javítsa ki az alábbi hibákat!',
			'body'    => $body,
			'height'  => count( $errors) * 25 + 10
		);
		
		array_unshift( $panels, $error_panel);
				
	} else {
		
		$db->sql = "UPDATE users SET bejnev='$p_bejnev',nev='$p_nev',telefon='$p_telefon',
                    varos='$p_varos',regio='$p_regio',altkategoria='$p_rovat',weblap='$p_weblap'
				    WHERE id='".User::get_id()."'";                  
        $db->query();
		//debug($db->sql);

		User::set_props (
			array (
				'username' => $p_bejnev,
				'name'     => $p_nev,
				'telefon'  => $p_telefon,
				'varos'    => $p_varos,
				'regio'    => $p_regio,
				'rovat'    => $p_rovat,
				'weblap'   => $p_weblap
			)
		);
					
		$panels = array ();
		
		$panel = array (
			'caption' => 'Információ',
			'title'   => 'Sikeres adatmódosítás!',
			'body'    => "<p class='success'>A profil adatait frissítettük!</p>",
			'height'  => '20'
		);

		array_push( $panels, $panel);
	}
}

//debug( $_SESSION,'session' );	
//debug( USER::get_prop ('rovat'), 'rovat' );
//debug( USER::get_prop ('regio'), 'regio' );
	    
?>

<?php include ("header.php"); ?>

<?php include ("top.php"); ?>

<?php include ("left.php"); ?>
							
<div id="middle">
	
<?php foreach( $panels as $panel) { ?>
	
	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption"><?php print $panel['caption']; ?></p>
				<p class="hptitle"><?php print $panel['title']; ?></p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="<?php print $panel['height']; ?>px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:<?php print $panel['height']; ?>px">
				<?php if ($panel['body'] != '') print $panel['body']; else { ?>
					<form name="form_profilom" id="form_profilom" method="post" enctype='application/x-www-form-urlencoded' accept-charset="utf-8">

						<label for='email' class='required'>Email</label>
						<input name='email' type='text' disabled='true' value="<?php print User::get_email(); ?>" />
						<br />
						
						<label for='bejnev'>Felhasználó név</label>
						<input name='bejnev' type='text' value="<?php print isset($p_bejnev)?$p_bejnev:User::get_username(); ?>" />
						<br />

						<label for='nev'>Teljes név</label>
						<input name='nev' type='text' value="<?php print isset($p_tnev)?$p_nev:User::get_prop('name'); ?>" />
						<br />
						
						<label for='telefon'>Telefon</label>
						<input name='telefon' type='text' value="<?php print isset($p_telefon)?$p_telefon:User::get_prop('telefon'); ?>" />
						<br />
						
						<label for='varos'>Város</label>
						<input name='varos' type='text' value="<?php print isset($p_varos)?$p_varos:User::get_prop('varos'); ?>" />
						<br />

						<label for='regio'>Régió</label>
						<select name='regio'>  
							<?php
								if (count($regiok) < 1) print "<option value='0'>Create a Régió first!</option>";
								foreach ($regiok as $regio) {
									$selected = '';
									if (isset( $p_regio)) {if ($p_regio == $regio['id']) $selected = "selected='selected'";}
									else if (User::get_prop( 'regio') == $regio['id']) $selected = "selected='selected'";
									print "<option value='" . $regio['id'] . "' $selected>" . $regio['nev'] . "</option>";
									$alregiok = $regio['childs'];
									if( is_array( $alregiok ) ) {
										foreach ($alregiok as $alregio) {
											$selected = '';
											if (isset( $p_regio)) {if ($p_regio == $alregio['id']) $selected = "selected='selected'";}
											else if (User::get_prop( 'regio') == $alregio['id']) $selected = "selected='selected'";
											print "<option value='" . $alregio['id'] . "' $selected>&nbsp;&nbsp;-&nbsp;" . $alregio['nev'] . "</option>";
										}
									}
								}
							?>                 
						</select>
						<br />

						<label for='rovat'>Gyakran használt rovat</label>
						<select name='rovat'>  
							<?php
								if (count($rovatok) < 1) print "<option value='0'>Create a Rovat first!</option>";
								foreach ($rovatok as $rovat) {
									$selected = '';
									if (isset( $p_rovat)) {if ($p_rovat == $rovat['id']) $selected = "selected='selected'";}
									else if (User::get_prop( 'rovat') == $rovat['id']) $selected = "selected='selected'";
									print "<option value='" . $rovat['id'] . "' $selected>" . $rovat['nev'] . "</option>";
									$alrovatok = $rovat['childs'];
									if( is_array( $alrovatok ) ) {
										foreach ($alrovatok as $alrovat) {
											$selected = '';
											if (isset( $p_rovat)) {if ($p_rovat == $alrovat['id']) $selected = "selected='selected'";}
											else if (User::get_prop( 'rovat') == $alrovat['id']) $selected = "selected='selected'";
											print "<option value='" . $alrovat['id'] . "' $selected>&nbsp;&nbsp;-&nbsp;" . $alrovat['nev'] . "</option>";
										}
									}
								}
							?>                 
						</select>
						<br />

						<label for='weblap'>Weblap</label>
						<input name='weblap' type='text' value="<?php print isset($p_weblap)?$p_weblap:User::get_prop('weblap'); ?>" />
						<br />
											
						<br />
											
						<label for="modosit">&nbsp;</label>
						<input name="modosit" type="submit" value="Módosít"/>				

					</form>  
				<?php } ?>           			
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="<?php print $panel['height']; ?>px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>

	<br />
	<br />

<?php } ?>

</div>

<?php include ("right.php"); ?>

<?php include ("footer.php"); ?>
